package bingo.component.amchart.service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import net.bingosoft.common.BeanHelper;
import net.bingosoft.common.ObjectRegistry;
import net.bingosoft.common.data.BaseDao;
import net.bingosoft.common.data.SQLProvider;
import net.bingosoft.common.data.SQLProvider.DynamicSQL;
import bingo.component.amchart.graphics.Graphic;
import bingo.component.amchart.graphics.Graphic.GraphicType;
import bingo.component.amchart.region.Data;
import bingo.component.amchart.region.Datas;
import bingo.component.amchart.region.Root;

/**
 * Jun 24, 2008 5:09:54 PM Author:yangdongyu(yangdy@bingosoft.net) ReadMe:
 * ChangeLog:
 */
public class RegionReaderService implements IGraphicReaderService {
	private BaseDao dao = ObjectRegistry.getBaseDao();

	public Graphic getGraphic(String sqlId, GraphicType type,
			Map<String, Object> params) throws IllegalArgumentException,
			IllegalAccessException, InvocationTargetException {

		DynamicSQL dSql = null;
		try {
			dSql = SQLProvider.getDynamicSQL(sqlId, params);
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Object[] sqlParam = dSql.getParams();
		String sql = dSql.getSql();
		
		Root root = new Root();
		List<Map<String, Object>> resultList = dao.list(sql,sqlParam);
		Datas datas = new Datas(type.name());
		for (Map<String, Object> result : resultList) {
			datas.addData(BeanHelper.Map2ObjectIgnoreCase(result, new Data()));
		}
		root.setDatas(datas);
		return root;
	}
}
